import router from './router'
import store from './store'
import NProgress from 'nprogress' // Progress 进度条
import 'nprogress/nprogress.css' // Progress 进度条样式
import { secondDirectory, tabList } from '@/utils/config.js'
import { getToken } from '@/utils/auth' // 验权
import {
    Message
} from 'element-ui'
import Layout from './views/layout/Layout'

const whiteList = ['/login'] // 不重定向白名单
router.beforeEach((to, from, next) => {
	console.log(to)
    NProgress.start()
    // 刷新判断当前是否登录状态
    let loginName = window.localStorage.getItem('admin') ? JSON.parse(window.localStorage.getItem('admin')) : ''
		console.log(secondDirectory)
    if (getToken('token') != 'undefined' && getToken()) {
				let menuList = JSON.parse(window.localStorage.getItem("menuList"));
				for(let i in secondDirectory) {
					console.log(i)
					menuList.forEach((item) => {
						if(item.name == i){
							console.log()
							item.childMenu.forEach((cel,index) =>　{
								secondDirectory[i].list[index].show = cel.select
							})
						}
					})
				}
        // 如果当前是登录路由 跳转首页
        to.path === '/login' ? next({
            path: '/'
        }) : next()
        NProgress.done()
        store.dispatch('SET_TAB_CURRENT_INDEX', 1)
        // 记录当前路由对象的 二级分类 和tabs显示
        store.dispatch('SET_SECOND_DIRECTORY', secondDirectory[to.name])
				console.log(to.name)
        store.dispatch('SET_TAB_LIST', tabList[to.name])
        
    } else {
        if (whiteList.indexOf(to.path) !== -1) {
            next()
        } else {
            next('/login')
            NProgress.done()
        }
    }
})

router.afterEach(() => {
    NProgress.done() // 结束Progress
})
